#import "ALTemplateEditorSplitViewController.h"


static const CGFloat ALTemplateEditorSplitViewControllerMinimumBottomViewHeight = 100.0f;
static const CGFloat ALTemplateEditorSplitViewControllerMinimumTopViewHeight = 100.0f;
NSString* const ALTemplateEditorIsCollapsedKey = @"ALTemplateEditorIsCollapsedKey";
static NSString* const ALTemplateEditorHeightKey = @"ALTemplateEditorHeightKey";


#pragma mark -
@interface ALTemplateEditorSplitViewController (ALTemplateEditorSplitViewControllerPrivateMethods)
-(void)setDividerPositionWithAnimation_:(BOOL)animateDivider;
-(void)setToggleButtonState_;
@end


#pragma mark -
@implementation ALTemplateEditorSplitViewController

@synthesize templateEditorSplitView;
@synthesize splitTopView;
@synthesize splitBottomView;
@synthesize templateEditorToggleButton;


#pragma mark -
#pragma mark Initialization

-(void)awakeFromNib {
	[self setDividerPositionWithAnimation_:NO];
	[self setToggleButtonState_];
}


#pragma mark -
#pragma mark Split view delegate

-(BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview {
	if (subview == [self splitBottomView] && [[NSUserDefaults standardUserDefaults] boolForKey:ALTemplateEditorIsCollapsedKey])
		return YES;
	return NO;
}

-(CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex {
	CGFloat splitViewHeight = [splitView frame].size.height;
	return splitViewHeight - ALTemplateEditorSplitViewControllerMinimumBottomViewHeight;
}

-(CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex {
	return ALTemplateEditorSplitViewControllerMinimumTopViewHeight;
}

-(BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex {
	return YES;
}

-(BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview {
	if (subview == [self splitBottomView])
		return NO;
	return YES;
}

-(void)splitViewDidResizeSubviews:(NSNotification *)aNotification {
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	BOOL templateEditViewIsCollapsed = [defaults boolForKey:ALTemplateEditorIsCollapsedKey];
	if (templateEditViewIsCollapsed)
		return;
	CGFloat templateEditorHeight = [[self splitBottomView] frame].size.height;
	[defaults setFloat:(float)templateEditorHeight forKey:ALTemplateEditorHeightKey];
}


#pragma mark -
#pragma mark Actions

-(IBAction)toggleTemplateEditor:(id)sender {
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	BOOL templateEditViewIsCollapsed = [defaults boolForKey:ALTemplateEditorIsCollapsedKey];
	[defaults setBool:!templateEditViewIsCollapsed forKey:ALTemplateEditorIsCollapsedKey];
	[self setDividerPositionWithAnimation_:YES];
	[self setToggleButtonState_];
}


#pragma mark -
#pragma mark Private

-(void)setDividerPositionWithAnimation_:(BOOL)animateDivider {
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	BOOL templateEditViewIsCollapsed = [defaults boolForKey:ALTemplateEditorIsCollapsedKey];
	CGFloat splitViewHeight = [[self templateEditorSplitView] frame].size.height;
	NSSize splitTopSize = [[self splitTopView] frame].size;
	NSSize splitBottomSize = [[self splitBottomView] frame].size;
	
	if (templateEditViewIsCollapsed) {
		splitTopSize.height = splitViewHeight;
		splitBottomSize.height = 0;
	}
	else {
		CGFloat templateEditorHeight = [defaults floatForKey:ALTemplateEditorHeightKey];
		splitTopSize.height = splitViewHeight - templateEditorHeight - [[self templateEditorSplitView] dividerThickness];
		splitBottomSize.height = templateEditorHeight;
	}
	if (animateDivider) {
		[[[self splitTopView] animator] setFrameSize:splitTopSize];
		[[[self splitBottomView] animator] setFrameSize:splitBottomSize];
	}
	else {
		[[self splitTopView] setFrameSize:splitTopSize];
		[[self splitBottomView] setFrameSize:splitBottomSize];
	}

}

-(void)setToggleButtonState_ {
	BOOL templateEditViewIsCollapsed = [[NSUserDefaults standardUserDefaults] boolForKey:ALTemplateEditorIsCollapsedKey];
	[[self templateEditorToggleButton] setState:templateEditViewIsCollapsed ? NSOffState : NSOnState];
}

@end
